package cn.yh.register.hosp.service.impl;

import cn.yh.register.client.DictFeignClient;
import cn.yh.register.enums.DictEnum;
import cn.yh.register.hosp.repository.HospitalRepository;
import cn.yh.register.hosp.service.HospitalService;
import cn.yh.register.model.hosp.BookingRule;
import cn.yh.register.model.hosp.Hospital;
import cn.yh.register.vo.hosp.HospitalQueryVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    HospitalRepository hospitalRepository;
    @Autowired
    DictFeignClient dictFeignClient;


    /**
     * 保存医院信息
     * @param objectMap 医院信息
     */
    @Override
    public void saveHospital(Map<String, Object> objectMap) {

        //把数据保存到mongoDB中 先转成JSON数据
        String jsonString = JSONObject.toJSONString(objectMap);
        //再把JSON转成对象
        Hospital hospital = JSONObject.parseObject(jsonString, Hospital.class);
        //看mongoDB中是否有此数据 有就修改 没有就添加
        Hospital hoscode = hospitalRepository.findByHoscode(hospital.getHoscode());
        if (hoscode != null) {
            //修改
            hospital.setId(hoscode.getId());
            hospital.setStatus(hoscode.getStatus());
            hospital.setCreateTime(hoscode.getCreateTime());
            hospital.setIsDeleted(hoscode.getIsDeleted());
        } else {
            //新增
            //状态 0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
        }
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    /**
     * 查询医院信息
     * @param hoscode 医院编号
     * @return 医院信息
     */
    @Override
    public Hospital getHospital(String hoscode) {

        return hospitalRepository.findByHoscode(hoscode);
    }

    /**
     * 分页查询医院列表
     * @param page    当前页
     * @param size    每页条数
     * @param queryVo 查询条件
     * @return 分页数据
     */
    @Override
    public Page<Hospital> findHospitalPage(Integer page, Integer size, HospitalQueryVo queryVo) {
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(queryVo, hospital);

        //Example
        Example<Hospital> example = Example.of(hospital, ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true));
        //PageRequest
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        //分页查询
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageRequest);
        //数据处理
        hospitalPage.getContent().forEach(this::packHospital);
        return hospitalPage;

    }

    /**
     * 更新状态
     * @param id     医院id
     * @param status 医院状态
     */
    @Override
    public void updateStatus(String id, Integer status) {
        Optional<Hospital> optional = hospitalRepository.findById(id);
        if (optional.isPresent()) {
            Hospital hospital = optional.get();
            hospital.setStatus(status);
            hospitalRepository.save(hospital);
        }

    }

    /**
     * 查询医院详情
     * @param id 医院id
     * @return 医院信息 & 详情规则
     */
    @Override
    public Map<String, Object> show(String id) {
        Optional<Hospital> optional = hospitalRepository.findById(id);
        if (optional.isPresent()) {
            Hospital hospital = optional.get();
            this.packHospital(hospital);
            //预约规则
            BookingRule bookingRule = hospital.getBookingRule();
            this.packHospital(hospital);
            //预约规则单独返回 所以医院对象中不需要返回
            hospital.setBookingRule(null);
            Map<String, Object> map = new HashMap<>();
            map.put("hospital", hospital);
            map.put("bookingRule", bookingRule);
            return map;
        }


        return null;
    }

    /**
     * 模糊查询
     * @param hosname 医院名字
     * @return 医院列表
     */
    @Override
    public List<Hospital> findByName(String hosname) {
        return hospitalRepository.findByHosnameLike(hosname);

    }

    /**
     * 查看医院详情
     * @param hoscode 医院编码
     * @return 医院信息 & 预约规则
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        //预约规则
        BookingRule bookingRule = hospital.getBookingRule();
        //预约规则单独返回
        hospital.setBookingRule(null);
        //处理数据
        this.packHospital(hospital);
        Map<String ,Object> map  = new HashMap<>();
        map.put("hospital", hospital);
        map.put("bookingRule", bookingRule);
        return map;
    }

    //处理对象数据
    private void packHospital(Hospital hospital) {
        // 医院类型
        String hosType = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        // 省
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        // 市
        String city = dictFeignClient.getName(hospital.getCityCode());
        // 区
        String district = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString", hosType);
        hospital.getParam().put("fullAddress", province + city + district + hospital.getAddress());
    }

}
